#ifndef BAS_FUN_DEFINITIONS
#define BAS_FUN_DEFINITIONS

#define CPU_HZ      12000000  //6000000

//#define AGL_FUN
//#define CDR_FUN 
#define DBG_FUN
#define SMB_RS
//#define SM_RS
//#define MMB_RS

//#define SDNP3
//#define WEB_CAM_FUN
#define GPS_NTPB   
#define PLC_FUN
//#define WEB_FUN
#define AEC_FUN

//#define LPC2368
#define LPC2378
//#define LPC2378_S
//#define LPC2388

#define iEC_FILE

#ifdef iEC_FILE   
#define IEMS_UNIX
#define VER_H        2
#else
#define VER_H        1
#endif

#define VER_L        0
#define VER_YY    2011 
#define VER_MN       1 
#define VER_DD      12 


#define VER_MKS     "AEC-32"
#ifdef iEC_FILE
#define HEX_FILE    "aec32.iEC"
#else
#define HEX_FILE    "aec32.hex"
#endif


#define PLC_FILE    "plc.bin"
#define RUN_FILE    "run.txt"

#define FLASH_PLC_ADDR    0x60000
#define FLASH_RUN_ADDR    0x68000

#if (CPU_HZ==12000000) 
#define FOR_1MS   1153      // NO-MAM
#else 
#if (CPU_HZ==6000000) 

#endif
#endif

typedef char                   S8;
typedef unsigned char          U8;
typedef short                  S16;
typedef unsigned short         U16;
typedef int                    S32;
typedef unsigned int           U32;
typedef long long              S64;
typedef unsigned long long     U64;
typedef unsigned char          BIT;
typedef unsigned int           BOOL;

#ifdef FIRST
#define EXTERN 
const char b1mask[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
const char b0mask[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

#else
#define EXTERN  extern
EXTERN  const char b1mask[8];
EXTERN  const char b0mask[8];
#endif
EXTERN U8  sec_rdy,net_rdy;   
EXTERN U8  sw2,sw3,sw8,sw9,sw_soe_ena,sys_init,sw_mci_use,mrs_id;
EXTERN U16 sw_gps_run,gps_unsync;

EXTERN U8  Tmn,Tdd,Thh,Tmm,Tss,Tww;
EXTERN U16 Tyy,tick_rdy,ms_count,net_ms;
EXTERN S16 Tms,oTms,Tmsofs;
EXTERN S32 Tsecs,Rsecs;
EXTERN U8  Lyy,Lmn,Ldd,Lhh,Lmm;
EXTERN U32 tdelay;
EXTERN U8  mns_sw;
EXTERN S8  sys_site[22];
EXTERN S8  sys_location[22];	 
EXTERN S8  lcd_key;
EXTERN U16 Tsync_sec,Tsync_alarm;
#define TSYNC_SECS 3600

EXTERN U8  ioport_stop;
#define IOPORT(p)  (ioport_stop=!p)

//#define MASTER_SLAVE  
#ifdef MASTER_SLAVE  
#define SW_MASTER   0
#define SW_SLAVE    1
EXTERN U8  sw_ms,ms_stus;
#define MASTER_TO_SLAVE_DLYMS   1000  // ms

#endif

void  initgpio(void);
void  outport(U8 address,U8 data);
U8    inport(U8 address);

void  init_timer0(void);
void  init_timer1(void);
void  tsleep(unsigned short tcnt);

void  for_delay(U32 count);
void  for_ms_test(int tms);
U8    Ibit(U8 *p,U16 ch);
void  Isetbit(U8 *p,U16 ch,U8 stus);

////////////////
#ifndef LPC2368
#define EX_SRAM
#ifdef EX_SRAM

#ifdef  LPC2388

#define EX0_USE_P0 { IOCLR1 |= 0x04000000; FIO2CLR |= 0x00000100; }
#define EX0_USE_P1 { IOSET1 |= 0x04000000; FIO2CLR |= 0x00000100; }
#define EX0_USE_P2 { IOCLR1 |= 0x04000000; FIO2SET |= 0x00000100; }
#define EX0_USE_P3 { IOSET1 |= 0x04000000; FIO2SET |= 0x00000100; }

#define EX1_USE_P0 { IOCLR1 |= 0x10000000; IOCLR1  |= 0x20000000; }
#define EX1_USE_P1 { IOSET1 |= 0x10000000; IOCLR1  |= 0x20000000; }
#define EX1_USE_P2 { IOCLR1 |= 0x10000000; IOSET1  |= 0x20000000; }
#define EX1_USE_P3 { IOSET1 |= 0x10000000; IOSET1  |= 0x20000000; }

#else   // LPC2378 
#define EX0_USE_P0  (IOCLR1|=0x08000000)
#define EX0_USE_P1  (IOSET1|=0x08000000)
#define EX1_USE_P0  (IOCLR1|=0x00400000)
#define EX1_USE_P1  (IOSET1|=0x00400000)
#endif

int  ex_sram_addr(int bno,int ofs_num,int dsize);

/////////////////////////////////////////////////
#define EX0_SRAM     0x80000000  // use  0x10000
#define UTR_BUF_AP   0x80000000  
#define UTR_BUF_SIZE 0x1600 
#define NTR_BUF_AP   (UTR_BUF_AP+UTR_BUF_SIZE)
#define NTR_BUF_SIZE 0x3000  
#define SYS_OTH_AP   (NTR_BUF_AP+NTR_BUF_SIZE)
#define SYS_OTH_SIZE 0x0200
#define SS_MEM_AP    (SYS_OTH_AP+SYS_OTH_SIZE)
#define SS_MEM_SIZE  0xB800
#define EX0_END_AP   (SS_MEM_AP+SS_MEM_SIZE) 

#if (EX0_END_AP>0x80010000)
EX0_error;
#endif 

#define EX1_SRAM      0x81000000  //USE=0x10000

#define MCI_MEM_AP    (EX1_SRAM)
#define MCI_MEM_SIZE  0x1000
#define MMB_DATA_AP   (MCI_MEM_AP+MCI_MEM_SIZE)  
#define MMB_DATA_SIZE 0x8000
#define EX1_END_AP    (MMB_MEM_AP+MMB_MEM_SIZE)  
/*
#define AGL_MEM_AP    (MCI_MEM_AP+MCI_MEM_SIZE)  
#define AGL_MEM_SIZE  0x1000
#define CD_DATA_AP    (AGL_MEM_AP+AGL_MEM_SIZE)  
#define CD_DATA_SIZE  0x2000
#define CDC_DATA_AP   (CD_DATA_AP+CD_DATA_SIZE)  
#define CDC_DATA_SIZE 0x5000
#define CDC_DATA_AP   (CD_DATA_AP+CD_DATA_SIZE)  
#define EX1_END_AP    (CDC_DATA_AP+CDC_DATA_SIZE)  
*/
/*
#define SD3_DATA_AP    (AGL_MEM_AP+AGL_MEM_SIZE)  
#define SD3_DATA_SIZE  0x2800
#define SD3_EVNB_AP    (SD3_DATA_AP+SD3_DATA_SIZE)  
#define SD3_EVNB_SIZE  0x2000
#define SD3_PMEM_AP    (SD3_EVNB_AP+SD3_EVNB_SIZE)  
#define SD3_PMEM_SIZE  0x1800
#define EX1_END_AP     (SD3_PMEM_AP+SD3_PMEM_SIZE)  
*/

#if (EX1_END_AP>0x81010000)
EX1_error;
#endif 


#endif  // EX_SRAM
#endif  

#define WDOG
#ifdef WDOG
void  TrigWD(void);
void  init_wd(void);
void  wdog_action(void);
#endif
EXTERN U16 WDogCount;

void eboot_flag(void);
void eboot_hex(char *fname);
void eboot_file(char *fname,int fladdr,int i2c_crc,int i2c_size,int backup);

int  time_secs(int *secs);
int  datetime_secs(U16 yy,U8 mn,U8 dd,U8 hh,U8 mm,U8 ss);
void secs_datetime(S32 secs,U16 *yy,U8 *mn,U8 *dd,U8 *hh,U8 *mm,U8 *ss);
#define RTC_USE   
#ifdef RTC_USE
void  init_rtc(void);
void  RTC_setime(U16 yy,U8  mn,U8 dd,U8 hh,U8 mm,U8 ss);
void  RTC_getime(void);
#endif 

#define I2C_NUM  1024
#ifdef I2C_NUM

extern unsigned short I2C_addr;
extern unsigned char  *I2C_buf;
extern unsigned short I2C_dsize;
extern unsigned char  I2C_flag;
#define I2C_FLAG_READ    1
#define I2C_FLAG_WRITE   2

void I2C_write_pro(unsigned short addr,unsigned char *data,unsigned short len);
void I2C_read_pro(unsigned short addr, unsigned char *data,unsigned short len);
void I2C_WriteBytes(unsigned short addr,unsigned char *data,unsigned short dlen);
void I2C_ReadBytes(unsigned short addr, unsigned char *data,unsigned short dlen);

#define I2C_MyMAC        0  // 6  Bytes
#define I2C_MyIP         6  // 4  Bytes
#define I2C_GatewayIP   10  // 4  Bytes
#define I2C_SubnetMask  14  // 4  Bytes
//#define I2C_MyMAC2      20  // 6  Bytes
#define I2C_MyIP2       26  // 4  Bytes
#define I2C_GatewayIP2  30  // 4  Bytes
#define I2C_SubnetMask2 34  // 4  Bytes
#define I2C_MRS_BAUD    50  // 8 Bytes  =4*2(SMB_NUM)
#define I2C_SDNP3_IDX   60  // 32 Bytes

#define I2C_AD_BAS     118  // 64 Bytes =4*16(AIH_NUM)
//#define I2C_SM_FC      182  // 16 Bytes = SM_NUM
//#define I2C_SM_DIN     198  // 32 Bytes =2*16(SM_NUM) 
//#define I2C_SM_DON     230  // 32 Bytes =2*16(SM_NUM) 
//#define I2C_SM_AIN     262  // 32 Bytes =2*16(SM_NUM) 

#define I2C_PLC_DCRC   294  // 2 Bytes
#define I2C_PLC_DSIZE  296  // 2 Bytes

#define I2C_MRS_PORT   300  // 4 Bytes  =2*2(SMB_NUM)
#define I2C_MRS_PROT   304  // 4 Bytes  =2*2(SMB_NUM)
#define I2C_FILE_DCRC  308  // 2  Bytes
#define I2C_FILE_DSIZE 310  // 2  Bytes

#define I2C_SDR_PORT   316    // 4 Bytes  =2*2(CDR_NUM)
#define I2C_SDR_PROT   320    // 4 Bytes  =2*2(CDR_NUM)
#define I2C_WEBCAN_DI    324  // 2 Bytes 
#define I2C_WEBCAN_DO    326  // 2 Bytes 
#define I2C_WEBCAN_DI_LG 328  // 2 Bytes 
#define I2C_PWR_CRC      340  // 2 Bytes
#define I2C_PLC_PWR      342  // 8 Bytes

#define I2C_AEC_START    400

#define	HW_PW_ADDR     0x378		// Hardware-Password (4 Bytes)

#define	HW_MODULE      0x380		// Hardware-Module Start Address (16 Bytes)
#define	HW_VERSION     0x390		// Hardware-Version Start Address (16 Bytes)
#define	HW_STATION     0x3A0		// Hardware-Station Start Address (20 Bytes)

#define	HW_LOCATION    0x3B8		// Hardware-Location Start Address (20 Bytes)

/*
#define	MYIP1ADDR_0    0x3D0		// MY IP-1 Address (4 Bytes)
#define	MYNM1ADDR_0    0x3D4		// MY NetMask-1 Address (4 Bytes)
#define	MYGW1ADDR_0    0x3D8		// MY Gateway-1 Address (4 Bytes)
#define	MYIP2ADDR_0    0x3E0		// MY IP-2 Address (4 Bytes)
#define	MYNM2ADDR_0    0x3E4		// MY NetMask-2 Address (4 Bytes)
#define	MYGW2ADDR_0    0x3E8		// MY Gateway-2 Address (4 Bytes)

//////////////////////////////////////////////////
#define	CONFIGADDR     0x3F0		// Config Start Address (16 Bytes)

#define	PDTSNADDR_0    0x3F4		// Product Serial Number (6 Bytes)
#define	PDTSNADDR_1    0x3F8		// Product Serial Number (6 Bytes)
#define	PDTSNADDR_2    0x3F6		// Product Serial Number (6 Bytes)
#define	PDTSNADDR_3    0x3F1		// Product Serial Number (6 Bytes)
#define	PDTSNADDR_4    0x3FF		// Product Serial Number (6 Bytes)
#define	PDTSNADDR_5    0x3F9		// Product Serial Number (6 Bytes)
*/
#define NICMACADDR_0   0x3FA 	    // NIC MAC Address (6 Bytes)
#define NICMACADDR_1   0x3FD 	    // NIC MAC Address (6 Bytes)
#define NICMACADDR_2   0x3F0 	    // NIC MAC Address (6 Bytes)
#define NICMACADDR_3   0x3F3 	    // NIC MAC Address (6 Bytes)
#define NICMACADDR_4   0x3FB  	    // NIC MAC Address (6 Bytes)
#define NICMACADDR_5   0x3F2  	    // NIC MAC Address (6 Bytes)
/*
#define CRCADDR_0      0x3F7  	    // Config CRC (2 Bytes)
#define CRCADDR_1      0x3FC  	    // Config CRC (2 Bytes)
#define	CFGCNTADDR     0x3FE		// Config Count (1 Bytes)
#define	CFGFLAGADDR    0x3F5		// Config Flag (1 Bytes)
*/

#endif


#define MRS_NUM  2
typedef struct { 
     unsigned short portno;
	 unsigned short protocol;
     unsigned short prot;
     short          path;	 
	 int            baud;
} MRS_PARM;
EXTERN MRS_PARM  MRS[MRS_NUM];
EXTERN U16 mrs_count;

#define   FLASH_FUN
#ifdef FLASH_FUN
extern unsigned long WFLASH_addr;
extern unsigned char *WFLASH_buf;
extern unsigned int  WFLASH_wsize;
extern unsigned char WFLASH_flag;
extern int           WFLASH_ret;

//#define WFLASH_FLAG_EW    1
//int flash_write(unsigned long flash_addr,unsigned char *buf,unsigned int wsize);
//int wflash_write(unsigned long flash_addr,unsigned char *buf,unsigned int wsize);
#define WFLASH_FLAG_PEW   2
int flash_page_write(unsigned long flash_addr,unsigned char *buf,unsigned int wsize);
int wflash_page_write(unsigned long flash_addr,unsigned char *buf,unsigned int wsize);
#define WFLASH_FLAG_E     3
int flash_erase(unsigned long flash_addr);
int wflash_erase(unsigned long flash_addr);
#endif

#define CRC16
#ifdef CRC16
short get_crc(unsigned char *s,short slen);
short get_crc2(short remainder,unsigned char *s,short slen);
short int_get_crc2(short remainder,unsigned char *s,short slen);
EXTERN unsigned char *CRC2_s;
EXTERN unsigned int  CRC2_slen;
EXTERN unsigned char CRC2_flag;
EXTERN short         CRC2_rcrc;
#endif

#define  MCI_FUN
#ifdef MCI_FUN
// check lpc2300.s =>USR_Stack_Size  EQU     0x00000800
// check U64, S64 define

#ifndef NULL
#define NULL    ((void *)0)
#endif

#ifndef FALSE
#define FALSE   (0)
#endif

#ifndef TRUE
#define TRUE    (1)
#endif

EXTERN  int mci_dpath;

#endif


#define _RS_SEND     1
#define _RS_REVOK    2
//#define _RS_FAIL     3
#define _RS_NOECHO   4
#define _RS_FMTERR   5
//#define _RS_TOERR    6
#define _RS_CRCERR   7 

////////////////////////////////
#define FILE_UD

#ifdef FILE_UD
typedef struct { 
	 short  path;
     int    portno;
     char   pnetip[20];
     short  ptcpudp;
	 unsigned short  step;
     unsigned short  size;
	 unsigned short  dly;	 
} FUD_PARM;
#define FUD_NUM   1
EXTERN FUD_PARM   FUD[FUD_NUM];
EXTERN unsigned short fud_dly;
void init_fud(short lno,int portno,unsigned char *ip,short prot);
void fud_pro(void);
#endif

//////////////////////////

#define ELNUM 256
short c_hex(unsigned char c);
char  hex_c(unsigned char hex);
char  getint(char *s,int *i,int *cint);
unsigned int hatoi(char *s);
char  getfloat(char *s,int *i,float *fv);
char  gethex(char *s,unsigned char *i,int *cint);
char  *bin_str(unsigned char c);
int   getdstr(char *s,int *i,char *cstr,char dc);
void  real_pro(void);
void  real_task(void);
void  gps_deg(void);
void  real_ms(void);

#endif

#ifdef AEC_FUN
//#define ENCODER
#define UPD_RST(x)  ((x) ? (IOSET0 = 0x00000200) : (IOCLR0=0x00000200));
EXTERN int    encoder_val;
EXTERN int    NowHeight;
EXTERN int    EncoderSpeed;
EXTERN int    Level3_dist_mm;
EXTERN char   Level_stus;
EXTERN int    last_level_m;

#define LEVEL_NO     0
#define LEVEL_U1     1
#define LEVEL_U2     3
#define LEVEL_FULL   7
#define LEVEL_D2     6
#define LEVEL_D1     4 	 
// => 000-001-011-111-110-100-000 <=

#define ENCODER_BUFF_TICK  100   //ms
#define ENCODER_BUFF_NUM    10   

typedef struct { 
    int  EncoderSpeed;
	char LevelData;
} ENCODER_PARM;

EXTERN ENCODER_PARM  ENC[ENCODER_BUFF_NUM];

void  encoder_rst(void); 
void  encoder_input(int);
void  init_extint3(void);
#endif
